/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.modules.act.service.ActTaskService;
import com.thinkgem.jeesite.modules.stock.dao.WarehouseLocDao;
import com.thinkgem.jeesite.modules.stock.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.dao.OutOrderDao;
import com.thinkgem.jeesite.modules.stock.dao.OutOrderDetailDao;

/**
 * 出库Service
 * @author 出库
 * @version 2019-12-21
 */
@Service
@Transactional(readOnly = true)
public class OutOrderService extends CrudService<OutOrderDao, OutOrder> {

	@Autowired
	private OutOrderDetailDao outOrderDetailDao;
	
	public OutOrder get(String id) {
		OutOrder outOrder = super.get(id);
		outOrder.setOutOrderDetailList(outOrderDetailDao.findList(new OutOrderDetail(outOrder)));
		return outOrder;
	}

	@Autowired
	private ActTaskService actTaskService;
	public List<OutOrder> findList(OutOrder outOrder) {
		return super.findList(outOrder);
	}
	
	public Page<OutOrder> findPage(Page<OutOrder> page, OutOrder outOrder) {
		return super.findPage(page, outOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(OutOrder outOrder) {
		{
			if (StringUtils.isBlank(outOrder.getId())) {
				outOrder.preInsert();
				dao.insert(outOrder);
				// 启动流程
				actTaskService.startProcess("out_order", "s_out_order", outOrder.getId(), "出库审批");

			}
			else{
				outOrder.preUpdate();
				dao.update(outOrder);
				outOrder.getAct().setComment(("yes".equals(outOrder.getAct().getFlag())?"[重申] ":"[销毁] ")+"入库审批");

				// 完成流程任务
				Map<String, Object> vars = Maps.newHashMap();
				vars.put("pass", "yes".equals(outOrder.getAct().getFlag())? "1" : "0");
				actTaskService.complete(outOrder.getAct().getTaskId(), outOrder.getAct().getProcInsId(), outOrder.getAct().getComment(), "入库审批", vars);

			}
			for (OutOrderDetail outOrderDetail : outOrder.getOutOrderDetailList()){
				if (outOrderDetail.getId() == null){
					continue;
				}
				if (OutOrderDetail.DEL_FLAG_NORMAL.equals(outOrderDetail.getDelFlag())){
					if (StringUtils.isBlank(outOrderDetail.getId())){
						outOrderDetail.setOrder(outOrder);
						outOrderDetail.preInsert();
						outOrderDetailDao.insert(outOrderDetail);
					}else{
						outOrderDetail.preUpdate();
						outOrderDetailDao.update(outOrderDetail);
					}
				}else{
					outOrderDetailDao.delete(outOrderDetail);
				}
			}

		}


	}
	
	@Transactional(readOnly = false)
	public void delete(OutOrder outOrder) {
		super.delete(outOrder);
		outOrderDetailDao.delete(new OutOrderDetail(outOrder));
	}
	@Autowired
	private StockService stockService;
	@Autowired
	private ContainerService containerService;
	@Autowired
	private ContainerDetailService containerDetailService;
	@Autowired
	private WarehouseLocDao warehouseLocDao;

	@Autowired
	private StockWarehouseService stockWarehouseService;
	@Transactional(readOnly = false)
    public void auditSave(OutOrder outOrder) {
		// 设置意见
		outOrder.getAct().setComment(("yes".equals(outOrder.getAct().getFlag())?"[同意] ":"[驳回] ")+outOrder.getAct().getComment());

		outOrder.preUpdate();

		// 对不同环节的业务逻辑进行操作
		String taskDefKey = outOrder.getAct().getTaskDefKey();

		// 审核环节
		if ("audit1".equals(taskDefKey)){

		}
		else if ("audit2".equals(taskDefKey)){
			if("yes".equals(outOrder.getAct().getFlag())){
				for (OutOrderDetail outOrderDetail : outOrder.getOutOrderDetailList()) {
					outOrderDetailDao.updateCid(outOrderDetail);
				}
			}
		}
		else if ("audit3".equals(taskDefKey)){

		}
		else if ("audit4".equals(taskDefKey)){

		}
		else if ("audit5".equals(taskDefKey)){

		}
		else if ("audit6".equals(taskDefKey)){

			if("yes".equals(outOrder.getAct().getFlag())){
				for (OutOrderDetail outOrderDetail : outOrder.getOutOrderDetailList()) {
					Stock s = new Stock();
					s.setW(outOrderDetail.getWoods());
					List<Stock> ss=stockService.findList(s);
					if(ss!=null&&ss.size()>0) {
						Stock stock = ss.get(0);
						stock.setNumber(stock.getNumber() - outOrderDetail.getPlannumber());
						stockService.save(stock);
					}
					ContainerDetail cd=outOrderDetail.getCid();
					ContainerDetail containerDetail=containerDetailService.get(cd.getId());
					Container container=containerService.get(containerDetail.getC().getId());
					double a=0d;
					if(container.getCtype().equals("1")){
						a=outOrderDetail.getPlannumber()/10d;
					}else{
						a=outOrderDetail.getPlannumber()/4d;
					}
					BigDecimal number = new BigDecimal(a);
					int newNumber = number.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();

					container.setNumber(container.getNumber()-newNumber);
					containerService.save(container);

					containerDetail.setNumber(containerDetail.getNumber()-newNumber);
					containerDetail.setWnumber(containerDetail.getWnumber()-outOrderDetail.getPlannumber());
					containerDetailService.save(containerDetail);

					WarehouseLoc warehouseLoc=warehouseLocDao.get(container.getC().getId());
					StockWarehouse stockWarehouse=new StockWarehouse();
					stockWarehouse.setWid(outOrderDetail.getWoods());
					stockWarehouse.setWhid(warehouseLoc.getC());
					List<StockWarehouse> sws=stockWarehouseService.findList(stockWarehouse);
					if(sws!=null&&sws.size()>0){
						stockWarehouse=sws.get(0);
						stockWarehouse.setNumber(stockWarehouse.getNumber()-outOrderDetail.getFactNumber());
						stockWarehouseService.save(stockWarehouse);
					}

				}
			}
		}


		// 未知环节，直接返回
		else{
			return;
		}

		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		vars.put("pass", "yes".equals(outOrder.getAct().getFlag())? "1" : "0");
		actTaskService.complete(outOrder.getAct().getTaskId(), outOrder.getAct().getProcInsId(), outOrder.getAct().getComment(), vars);

	}
}